package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.utils.X3ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class X3SlideBar extends View {

    private List<String> BAR_LETTERS = new ArrayList<>();

    private Paint paint = new Paint();

    //选项的数组下标
    private int selectIndex = 0;

    private onTouchLetterListener onTouchLetterListener;

    //背景是否变化
    private boolean isShowBkg = false;

    public boolean isSelectWork = false;

    private Context mContext;
    private int singleHeight;

    public X3SlideBar(Context context) {
        this(context,null);
    }

    public X3SlideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3SlideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    public interface onTouchLetterListener {
        void onTouchLetterChange(String letter,boolean touch);
    }

    public void setOnTouchLetterListener(onTouchLetterListener onTouchLetterListener) {
        this.onTouchLetterListener = onTouchLetterListener;
    }

//    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (isSelectWork){
            // 获取宽-测量规则的模式和大小
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);

            // 获取高-测量规则的模式和大小
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            // 设置wrap_content的默认宽 / 高值
            // 默认宽/高的设定并无固定依据,根据需要灵活设置
            // 类似TextView,ImageView等针对wrap_content均在onMeasure()对设置默认宽 / 高值有特殊处理,具体读者可以自行查看
            int mWidth = X3ScreenUtils.dipToPx(22,getContext());
            int mHeight = ((BAR_LETTERS.size() + 1) * X3ScreenUtils.dipToPx(22,getContext())) + X3ScreenUtils.dipToPx(10,getContext());


            // 当模式是AT_MOST（即wrap_content）时设置默认值
            if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
                setMeasuredDimension(mWidth, mHeight);
                // 宽 / 高任意一个模式为AT_MOST（即wrap_content）时，都设置默认值
            } else if (widthMode == MeasureSpec.AT_MOST) {
                setMeasuredDimension(mWidth, heightSize);
            } else if (heightMode == MeasureSpec.AT_MOST) {
                setMeasuredDimension(widthSize, mHeight);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isShowBkg == true) {
            canvas.drawColor(Color.WHITE);//显示背景
        }
        if (BAR_LETTERS.size() == 0) {
            return;
        }
        int height = getHeight();
        int width = getWidth();
//        int singleHeight = (height - 20) / BAR_LETTERS.size();//每个字母占得高度
        singleHeight = X3ScreenUtils.dipToPx(22,mContext);

        for (int i = 0; i < BAR_LETTERS.size(); i++) {//遍历字母

            paint.setColor(Color.parseColor("#989AA2"));
            paint.setTextSize(X3ScreenUtils.dipToPx(10,mContext));
            paint.setAntiAlias(true);

            //从左下角开始绘制
            float xpos = (width - paint.measureText(BAR_LETTERS.get(i))) / 2;//x轴
            float ypos = singleHeight * i + singleHeight;//y轴 （i从0开始算）

            Rect bounds = new Rect();
            paint.getTextBounds(BAR_LETTERS.get(i), 0,BAR_LETTERS.get(i).length(), bounds);

            if (i == selectIndex) {
                paint.setColor(Color.parseColor("#FFFFFF"));
                canvas.drawCircle(getWidth() / 2,ypos - bounds.height() / 2, X3ScreenUtils.dipToPx(8,mContext),paint);

                //如果被选中
                paint.setColor(Color.parseColor("#333333"));
                paint.setFakeBoldText(true);
            }

            canvas.drawText(BAR_LETTERS.get(i), xpos, ypos, paint);//开始绘制
            //paint.reset();//重置画笔
        }
    }

    public void changeSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
        invalidate();
    }

    public void changeNum(List<String> mList){
        BAR_LETTERS = mList;
        invalidate();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float ypos = event.getY();

        //上一次触发的坐标
        int oldSelectIndex = selectIndex;
        //根据触摸的位置确定点的哪个字母
//        int newSelectIndex = (int) (ypos * BAR_LETTERS.size() / getHeight());
        int newSelectIndex = (int) (ypos / singleHeight);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                isShowBkg = false;
                if (onTouchLetterListener != null && oldSelectIndex != newSelectIndex
                        && newSelectIndex >= 0 && newSelectIndex < BAR_LETTERS.size()) {
                    if (onTouchLetterListener != null) {
                        if (newSelectIndex >= BAR_LETTERS.size()) {
                            newSelectIndex = BAR_LETTERS.size() - 1;
                        }
                        onTouchLetterListener.onTouchLetterChange(BAR_LETTERS.get(newSelectIndex),true);//注册点击事件并传入字母
                    }
                    oldSelectIndex = newSelectIndex;
                    selectIndex = newSelectIndex;
                    invalidate();//重新绘制

                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                isShowBkg = false;

                if (onTouchLetterListener != null) {
                    if (newSelectIndex >= BAR_LETTERS.size()) {
                        newSelectIndex = BAR_LETTERS.size() - 1;
                        selectIndex = newSelectIndex;
                    }
                    if (newSelectIndex < 0) {
                        newSelectIndex = 0;
                    }
                    onTouchLetterListener.onTouchLetterChange(BAR_LETTERS.get(newSelectIndex),false);//注册点击事件并传入字母
                }
                invalidate();
                getParent().requestDisallowInterceptTouchEvent(true);

                break;
            case MotionEvent.ACTION_MOVE:
                getParent().requestDisallowInterceptTouchEvent(true);
                if (onTouchLetterListener != null && oldSelectIndex != newSelectIndex
                        && newSelectIndex >= 0 && newSelectIndex < BAR_LETTERS.size()) {
                    if (onTouchLetterListener != null) {
                        onTouchLetterListener.onTouchLetterChange(BAR_LETTERS.get(newSelectIndex),true);//注册点击事件并传入字母
                    }

                    oldSelectIndex = newSelectIndex;
                    selectIndex = newSelectIndex;
                    invalidate();//重新绘制
                }
                break;
        }


        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}